package com.xc.shopping_font.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xc.shopping_font.common.Result;
import com.xc.shopping_font.entity.Goods;
import com.xc.shopping_font.mapper.GoodsMapper;
import com.xc.shopping_font.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Slf4j
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    GoodsMapper goodsMapper;

    @Resource
    GoodsService goodsService;

    @GetMapping("/detail/{id}")
    public Result getGoods(@PathVariable long id){

       return Result.success(goodsMapper.selectById(id));

    }

    @GetMapping("/getGoods")
    public IPage<Goods> page(@RequestParam Integer pageSize,@RequestParam Integer pageNum,@RequestParam(defaultValue = "")
            String productName){

        IPage<Goods> page = new Page<>(pageNum,pageSize);

        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();

        if(!"".equals(productName)){
            queryWrapper.like("name",productName);
        }

        queryWrapper.eq("on_sale",1);

        queryWrapper.orderByAsc("id");

      return   goodsService.page(page,queryWrapper);

    }





}
